package com.soundShape.utils
{
	import com.soundShape.events.SoundShapeEvent;
	import com.soundShape.mediators.MainViewMediator;
	import com.soundShape.mediators.comparison.ComparisonMediator;
	import com.soundShape.mediators.photos.PhotosMediator;
	import com.soundShape.mediators.reporting.ReportingMediator;
	import com.soundShape.model.ApplicationModel;
	
	import mx.collections.ArrayCollection;
	
	import org.as3commons.logging.ILogger;
	import org.as3commons.logging.LoggerFactory;

	public class LogoutUtil
	{
		
		/**
		 * Logger.
		 */
		private static const logger:ILogger = LoggerFactory.getClassLogger(LogoutUtil);
		
		[Inject]
		public var applicationModel:ApplicationModel;
		
		[Inject]
		public var mainViewMediator:MainViewMediator;
		
		[Inject]
		public var photosMediator:PhotosMediator;
		
		[Inject]
		public var reportingMediator:ReportingMediator;
		
		[Inject]
		public var comparisonMediator:ComparisonMediator;
		
		public function LogoutUtil()
		{
		}
		
		[EventHandler(event="SoundShapeEvent.SWITCH_TO_LOGIN_VIEW")]
		public function doLogout(soundShapeEvent:SoundShapeEvent):void
		{
			trace("************************************************************************************");
			trace("************************************** logout **************************************");
			trace("************************************************************************************");
			
			this.applicationModel.employeeVO = null;
			this.applicationModel.customerVO = null;
			this.applicationModel.treatmentVO = null;
			
			mainViewMediator.view.lblLogout.text = "";
			
			/* ******************************************************************* */
			/*                       clear photos view                             */ 
			/* ******************************************************************* */
			if(photosMediator.view != null)
			{
				try
				{
					ArrayCollection(photosMediator.view.lstImg.dataProvider).removeAll();
					ArrayCollection(photosMediator.view.lstImg.dataProvider).refresh();
				}
				catch(e:Error)
				{
					logger.error("npe in logout process");
				}
				
				photosMediator.view.txtAreaCurrentTreatment.text = "";
			}
			
			/* ******************************************************************* */
			/*                     clear reporting view                            */ 
			/* ******************************************************************* */
			if(reportingMediator.view != null)
			{
				try
				{
					ArrayCollection(reportingMediator.view.lstImg.dataProvider).removeAll();
					ArrayCollection(reportingMediator.view.lstImg.dataProvider).refresh();
				}
				catch(e:Error)
				{
					logger.error("npe in logout process");
				}
			}
			
			/* ******************************************************************* */
			/*                    clear comparison view                            */ 
			/* ******************************************************************* */
			if(comparisonMediator.view != null)
			{
				if(comparisonMediator.view.dgTreatments1.dataProvider != null)
				{
					ArrayCollection(comparisonMediator.view.dgTreatments1.dataProvider).removeAll();
					ArrayCollection(comparisonMediator.view.dgTreatments1.dataProvider).refresh();
				}
				
				if(comparisonMediator.view.dgTreatments2.dataProvider != null)
				{
					ArrayCollection(comparisonMediator.view.dgTreatments2.dataProvider).removeAll();
					ArrayCollection(comparisonMediator.view.dgTreatments2.dataProvider).refresh();
				}
				
				if(comparisonMediator.view.lstImg1.dataProvider != null)
				{
					ArrayCollection(comparisonMediator.view.lstImg1.dataProvider).removeAll();
					ArrayCollection(comparisonMediator.view.lstImg1.dataProvider).refresh();
				}
				
				if(comparisonMediator.view.lstImg2.dataProvider != null)
				{
					ArrayCollection(comparisonMediator.view.lstImg2.dataProvider).removeAll();
					ArrayCollection(comparisonMediator.view.lstImg2.dataProvider).refresh();
				}
			}
				
				
		}
	}
}